<?php
/**
* This class identify one object (file or folder) under the storage area.
*The website do have a storage area (located under <i>site_data/Storage</i>), this class
*holds informations on one of the files or folders located there. To access and allocate instances
*of StoredItem please refer to the <i>Storage</i> class.</p>
*<p>The following details are collected by this class:
*<code>$details = array("folder" => "name of containing(parent) folder relative to the storage area",
*					"name" => "name of object",
*					"filename" => "full path to the object (complete with site_data/storage part)",
*					"type" => "type of file: one of 'img' or 'doc' or 'unk' ro 'folder'",
*					"ext" => "windows-style extension of the object" );
*</code>
*If you need the full path use "filename", if you only need the relative then combine "folder" and "name".</p>
*<p>The relative path of the object (relative to the storag area, which is site_data/storage) is the ID of the
*object.</p>
*<p>The object type is guessed by the extension. Only a few types are identified so far.</p>
*<p>For folders, easy to retrieve the contents by calling the <i>GetLeaves()</i> method, it will scan the
*folder and load the contained items as StoreObjects themselves. This is done calling <i>Storage::Get()</i></p>
*<p>The fastest way to get the full path to the object is to cast the StoredItem item to string:
*<code>(string)$my_object</code> this is achieved by implementing the <i>__string</i> magic method.</p>
<p>A <b>preliminary</b> <i>Preview()</i> method allow for the generation of previews for images. The other
*file types are simply returned as hyperlinks.</p>
*@todo Support more filetypes and previews
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Core
*@subpackage Storage
*/
class StoredItem
{
	private $path = "";
	private $full_path = false;
	private $leaves = false;

	private $details = array("folder" => false,
						"name" => false,
						"filename" => false,
						"type" => "unk",
						"type_icon" => "ui-icon-help",
						"path" => "",
						"ext" => "" );
	/**
	* Return the full path to the object (complete with stored are path).
	*@return string The full path to the object
	*/
	public function __tostring()
	{
		return $this->full_path;
	}
	/**
	* Return the StoredItem for the parent folder. Will return false for the root folder.
	*@return StoredOBject The containing folder (or <i>false</i> if this is the root folder)
	*/
	public function GetParent()
	{
		if ( $this->path == "" )
			return false;
		$p = dirname( $this->path );
		return $p == "." ? false : Storage::Get($p);
	}
	/**
	* Return object id (wihch is the relative path to the storage area).
	*/
	public function GetId()
	{
		return $this->path;
	}
	/**
	* Allocate the object and scan all details on the given file/folder.
	*@param string $path The file/folder to analyze
	*/
	public function __construct( $path, $base_path )
	{
		$this->path = $this->details["path"] = $path;
		$this->full_path = "$base_path/".$path;

		if ( file_exists($this->full_path) )
		{
			$this->details["name"] = basename( $path );
			$this->details["folder"] = dirname( $path );
			$this->details["filename"] = $this->full_path;
	
			if ( !is_dir( $this->full_path ) )
			{
				static $filetypes = array( "img" => array("jpg",
												"jpeg",
												"png",
												"gif",
												"bmp"),
									"doc" => array("txt",
												"pdf",
												"doc",
												"odf",
												"xls"));
				$this->details["ext"] = substr($this->details["name"], strrpos($this->details["name"], '.') + 1);
				foreach ( $filetypes as $type => $exts )
					if ( in_array( $this->details["ext"], $exts ) )
						$this->details["type"] = $type;
			}
			else
				$this->details["type"] = "folder";
		}
	}
	/**
	* Return the details data structure on this file/folder.
	@return array See the description for more info
	*/
	public function GetDetails()
	{
		$ret = $this->details;
		$ret["full_path"] = $this->full_path;
		return $ret;
	}
	/**
	* Return the type of the item
	@return string type of item
	*/
	public function Type()
	{
		return $this->details["type"];
	}
	/**
	* Collect all contents of the folder (return false for files).
	*@return array Return array of StoredItems
	*/
	public function GetLeaves()
	{
		if ( file_exists($this->full_path) )
		{
			if ( !$this->leaves && ($this->details["type"] == "folder") )
			{
				$this->leaves = array();
				$f = CoreUtils::ListFolder( $this->full_path );
				foreach ( $f as $x )
					if ( ($tmp = Storage::Get( $this->path."/$x" )) !== false )
						$this->leaves[ $x ] = $tmp;
			}
			return $this->leaves;
		}
		return false;
	}
	/**
	* Given the name of an item inside the folder, return the StoredItem (return false if me is not a folder).
	*@param string $leaf Name of item (relative path to storage area, not simply filename)
	*@return StoredItem object with details on item
	*/
	public function GetLeaf( $leaf )
	{
		if ( $this->GetLeaves() && isset($this->leaves[$leaf]) )
			return $this->leaves[$leaf];
		return false;
	}
	/**
	* Experimental preview: return HTML code to preview the item (works only for images).
	*@return mixed HtmlTags
	*/
	public function Preview()
	{
		switch ( $this->details["type"] )
		{
			case "folder":
				$leaves = $this->GetLeaves();
				$html = new HtmlTable( count($leaves), 2 );
				foreach ( $leaves as $leave )
				{
					$html->Append( $row = new HtmlTableRow() );
					$row->Append( $td = new HtmlTableCell() );
					$td->Append( $leave->details["name"] );
					$row->Append( $td = new HtmlTableCell() );
				}
				break;

			case "img":
				$html = new HtmlImg( $this->full_path, $this->details["name"] );
				break;

			default:
				$html = new HtmlLink( $this->details["name"], $this->GetLink() );
		}
		return $html;
	}
}
?>